package com.xinqi.modules.databank.v2.util;

import com.alibaba.fastjson.JSONArray;
import com.xinqi.common.base.dto.CommonFileDTO;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.utils.json.JSON;
import com.xinqi.common.core.enums.CourseContentTypeEnum;
import com.xinqi.common.core.enums.InteractiveTypeEnum;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.modules.databank.dto.req.CourseResourceDataCreateDTO;
import com.xinqi.modules.databank.dto.req.CourseResourceDataUpdateDTO;
import com.xinqi.modules.databank.dto.req.question.CommonDataFileDTO;
import com.xinqi.modules.databank.dto.rsp.InteractiveDTO;
import com.xinqi.modules.databank.v2.domain.CourseResourceEntity;
import com.xinqi.modules.databank.v2.domain.CourseResourcePartEntity;
import org.apache.commons.collections4.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 资料库公共类
 * @author dzy
 */
public class ResourceUtil {

    /**
     * 校验发布状态 (定时发布，还是立即发布)
     */
    public static void checkTiming(CourseResourceEntity resource) {
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), resource.getNowRelease())) {
            resource.setReleaseTime(LocalDateTime.now());
            resource.setStatus(YesNoNumberEnum.YES.getCode());
        } else {
            resource.setStatus(YesNoNumberEnum.NO.getCode());
        }
    }

    /**
     * 校验时间
     */
    public static void checkTime(CourseResourceEntity resource) {

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 定时发布校验
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), resource.getTiming())) {
            CommonErrors.BAD_REQUEST.check(Objects.nonNull(resource.getReleaseTime()), MessageUtils.message("common.timing.time.is.not.blank"));
            CommonErrors.BAD_REQUEST.check(now.isBefore(resource.getReleaseTime()), MessageUtils.message("common.timing.time.is.valid"));
        }

        // 校验截止时间校验
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), resource.getStatus()) || Objects.equals(YesNoNumberEnum.YES.getCode(), resource.getTiming())) {
            // 校验截止时间不可为空
            CommonErrors.BAD_REQUEST.check(Objects.nonNull(resource.getEndTime()), MessageUtils.message("common.end.time.is.not.blank"));
            // 截止时间业务校验
            if (Objects.equals(YesNoNumberEnum.YES.getCode(), resource.getStatus()) || Objects.equals(YesNoNumberEnum.YES.getCode(), resource.getTiming())) {
                CommonErrors.BAD_REQUEST.check(Objects.nonNull(resource.getEndTime()), MessageUtils.message("common.end.time.is.not.blank"));
            }
            CommonErrors.BAD_REQUEST.check(now.isBefore(resource.getEndTime()), MessageUtils.message("common.end.time.is.valid"));
            // 发布时间不为空时与截止时间对比校验
            if (Objects.nonNull(resource.getReleaseTime())) {
                CommonErrors.BAD_REQUEST.check(resource.getReleaseTime().isBefore(resource.getEndTime()), MessageUtils.message("common.release.compare.end.time.is.valid"));
            }
        }


    }

    /**
     * 添加资料文件校验
     */
    public static void addCheckFile(CourseResourceDataCreateDTO dto) {
        if (CollectionUtils.isNotEmpty(dto.getFileUrls()) || CollectionUtils.isNotEmpty(dto.getVideoUrls())
            || CollectionUtils.isNotEmpty(dto.getImageUrls())) {
        } else {
            CommonErrors.BAD_REQUEST.check(false, MessageUtils.message("course.file.is.not.blank"));
        }
    }

    /**
     * 修改资料文件校验
     */
    public static void updateCheckFile(CourseResourceDataUpdateDTO dto) {
        if (CollectionUtils.isNotEmpty(dto.getFileUrls()) || CollectionUtils.isNotEmpty(dto.getVideoUrls())
            || CollectionUtils.isNotEmpty(dto.getImageUrls())) {
        } else {
            CommonErrors.BAD_REQUEST.check(false, MessageUtils.message("course.file.is.not.blank"));
        }
    }

    /**
     * 获取资料文件
     */
    public static List<CommonDataFileDTO> getDataFile(CourseResourceEntity entity) {
        List<CommonDataFileDTO> fileList = new ArrayList<>();
        if (Objects.nonNull(entity.getImages())) {
            fileList.addAll(JSON.parseList(entity.getImages(), CommonDataFileDTO.class));
        }
        if (Objects.nonNull(entity.getVideos())) {
            fileList.addAll(JSON.parseList(entity.getVideos(), CommonDataFileDTO.class));
        }
        if (Objects.nonNull(entity.getFileurls())) {
            fileList.addAll(JSON.parseList(entity.getFileurls(), CommonDataFileDTO.class));
        }
        return fileList;
    }

    /**
     * 阿里OSS获取视频图片
     */
    public static String generateVideoImgUrl(String videos, int timeline) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(videos),"视频数据不能为空");
        List<CommonFileDTO> videoList = JSONArray.parseArray(videos, CommonFileDTO.class);
        return videoList.get(0).getUrl()+"?x-oss-process=video/snapshot,t_"+timeline+",f_jpg";
    }

    /**
     * 资料文件转 成绩分析列表
     */
    public static List<InteractiveDTO> tranInteractive(CourseResourceEntity entity, List<CourseResourcePartEntity> partList
        , List<CommonDataFileDTO> fileList) {

        List<InteractiveDTO> list = new ArrayList<>();
        for (int i = 0; i < partList.size(); i++) {
            CourseResourcePartEntity part = partList.get(i);
            if (Objects.equals(InteractiveTypeEnum.QUESTION.getCode(), part.getInteractionType())) {
                InteractiveDTO dto = new InteractiveDTO();
                dto.setId(part.getId());
                dto.setContentId(entity.getId());
                dto.setContentType(entity.getContentType());
                if (Objects.equals(CourseContentTypeEnum.DATA.getCode(), entity.getContentType())) {
                    CommonDataFileDTO dataFile = fileList.stream().filter(v -> Objects.equals(part.getResourceId(), v.getId()))
                        .findFirst().orElse(null);
                    if (Objects.nonNull(dataFile)) {
                        dto.setName(Objects.isNull(dataFile.getName()) ? dataFile.getFilename() : dataFile.getName());
                        dto.setFileUrl(dataFile.getFileUrl());
                        dto.setType(dataFile.getType());
                        dto.setSize(dataFile.getSize());
                    }
                } else {
                    dto.setName("互动环节" + (i + 1));
                    dto.setType(0);
                    dto.setSize(0L);
                }
                list.add(dto);
            }
        }
        return list;
    }

}
